/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	
	function checkChar(target,charNum){
		var textbox = $.data(target, "textbox"),opts = textbox.options,regChars = opts.regChars;
		for(var i=0;regChars && i<regChars.length;i++){
			if(regChars[i].charCodeAt()==charNum){
				return true;
			}
		}
		return false;
	}
	//此方法只在此js的setValue方法中调用，格式化输入的值。
	function parser(opt,s) {
		if(s && typeof s == 'string'){
			var sarray = s.split("");
			var ns=[];
			for(var sr=0;sr<sarray.length;sr++){
				if(!checkChar(opt,sarray[sr].charCodeAt())){
					ns.push(sarray[sr]);
				}
			}
			return ns.join("");
		}
		return s;
	}
	
	function createTextbox(target) {
		$(target).addClass("textbox-f").hide();
		var textboxWrap = $("<span class=\"textbox\">"
		+ "<input class=\"textbox-text\" autocomplete=\"off\">"
		+ "<span class=\"textbox-addon\"><span class=\"textbox-icon\"></span></span>"
		+ "<input type=\"hidden\" class=\"textbox-value\">"
		+ "</span>").insertAfter(target);
		
		var name = $(target).attr("name");
		if (name) {
			textboxWrap.find("input.textbox-value").attr("name", name);
			$(target).removeAttr("name").attr("textboxName", name);
		}
		textboxWrap.bind("_resize", function(e, _bool) {
			if ($(this).hasClass("easyui-fluid") || _bool) {
				resize(target);
			}
			return false;
		}).bind("click",function(){
			var textbox = $.data(target, "textbox");
			var opts = textbox.options;
			opts.onClick.call(target);
		});
		return textboxWrap;
	};
	function initTextbox(target) {
		var textbox = $.data(target, "textbox");
		var opts = textbox.options;
		var tb = textbox.textbox;
		tb.find(".textbox-text").remove();
		if (opts.multiline) {
			$("<textarea class=\"textbox-text\" autocomplete=\"off\"></textarea>").prependTo(tb);
		} else {
			$("<input type=\"" + opts.type + "\" class=\"textbox-text\" autocomplete=\"off\">").prependTo(tb);
		}
		tb.find(".textbox-addon").remove();
		var bb = opts.icons ? $.extend(true, [], opts.icons) : [];
		if (opts.iconCls) {
			bb.push({
				iconCls : opts.iconCls,
				disabled : true
			});
		}
		if (bb.length) {
			var bc = $("<span class=\"textbox-addon\"></span>").prependTo(tb);
			bc.addClass("textbox-addon-" + opts.iconAlign);
			for (var i = 0; i < bb.length; i++) {
				bc.append("<a href=\"javascript:void(0)\" class=\"textbox-icon " + bb[i].iconCls + "\" icon-index=\"" + i + "\"></a>");
			}
		}
		tb.find(".textbox-button").remove();
		if (opts.buttonText || opts.buttonIcon) {
			var _b = $("<a href=\"javascript:void(0)\" class=\"textbox-button\"></a>").prependTo(tb);
			_b.addClass("textbox-button-" + opts.buttonAlign).linkbutton({
				text : opts.buttonText,
				iconCls : opts.buttonIcon,
				onClick : function() {
					opts.onClickButton.call(target);
				}
			});
		}
		setDisplayState(target, opts.disabled);
		readonly(target, opts.readonly);
	};
	function destroy(target) {
		var tb = $.data(target, "textbox").textbox;
		tb.find(".textbox-text").validatebox("destroy");
		tb.remove();
		$(target).remove();
	};
	function resize(target, width) {
		var textbox = $.data(target, "textbox");
		var opts = textbox.options;
		var tb = textbox.textbox;
		var parent = tb.parent();
		if (width) {
			opts.width = width;
		}
		if (isNaN(parseInt(opts.width))) {
			var c = $(target).clone();
			c.css("visibility", "hidden");
			c.insertAfter(target);
			opts.width = c.outerWidth();
			c.remove();
		}
		tb.appendTo("body");
		var text = tb.find(".textbox-text");
		var btn = tb.find(".textbox-button");
		var addon = tb.find(".textbox-addon");
		var icon = addon.find(".textbox-icon");
		tb._size(opts, parent);
		btn.linkbutton("resize", {
			height : tb.height()
		});
		btn.css({
			left : (opts.buttonAlign == "left" ? 0 : ""),
			right : (opts.buttonAlign == "right" ? 0 : "")
		});
		addon.css({
			left : (opts.iconAlign == "left" ? (opts.buttonAlign == "left" ? btn._outerWidth() : 0) : ""),
			right : (opts.iconAlign == "right" ? (opts.buttonAlign == "right" ? btn._outerWidth() : 0) : "")
		});
		icon.css({
			width : opts.iconWidth + "px",
			height : tb.height() + "px"
		});
		text.css({
			paddingLeft : (target.style.paddingLeft || ""),
			paddingRight : (target.style.paddingRight || ""),
			marginLeft : getPosition("left"),
			marginRight : getPosition("right")
		});
		if (opts.multiline) {
			text.css({
				paddingTop : (target.style.paddingTop || ""),
				paddingBottom : (target.style.paddingBottom || "")
			});
			text._outerHeight(tb.height());
		} else {
			var textHeight = Math.floor((tb.height() - text.height()) / 2);
			text.css({
				paddingTop : textHeight + "px",
				paddingBottom : textHeight + "px"
			});
		}
		text._outerWidth(tb.width() - icon.length * opts.iconWidth
				- btn._outerWidth());
		tb.insertAfter(target);
		opts.onResize.call(target, opts.width, opts.height);
		function getPosition(position) {
			return (opts.iconAlign == position ? addon._outerWidth() : 0) + (opts.buttonAlign == position ? btn._outerWidth() : 0);
		};
	};
	function validatebox(target) {
		var opts = $(target).textbox("options");
		var tb = $(target).textbox("textbox");
		tb.validatebox($.extend({}, opts, {
			deltaX : $(target).textbox("getTipX"),
			onBeforeValidate : function() {
				var box = $(this);
				if (!box.is(":focus")) {
					opts.oldInputValue = box.val();
					box.val(opts.value);
				}
			},
			onValidate : function(_1f) {
				var box = $(this);
				if (opts.oldInputValue != undefined) {
					box.val(opts.oldInputValue);
					opts.oldInputValue = undefined;
				}
				var tb = box.parent();
				if (_1f) {
					tb.removeClass("textbox-invalid");
				} else {
					tb.addClass("textbox-invalid");
				}
			}
		}));
	};
	function bindEvent(target) {
		var textbox = $.data(target, "textbox");
		var opts = textbox.options;
		var tb = textbox.textbox;
		var tbText = tb.find(".textbox-text");
		tbText.attr("placeholder", opts.prompt);
		tbText.unbind(".textbox");
		if (!opts.disabled && !opts.readonly) {
			tbText.bind("blur.textbox", function(e) {
				if (!tb.hasClass("textbox-focused")) {
					return;
				}
				opts.value = $(this).val();
				if (opts.value == "") {
					$(this).val(opts.prompt).addClass("textbox-prompt");
				} else {
					$(this).removeClass("textbox-prompt");
				}
				tb.removeClass("textbox-focused");
			}).bind("focus.textbox", function(e) {
				if ($(this).val() != opts.value) {
					$(this).val(opts.value);
				}
				$(this).removeClass("textbox-prompt");
				tb.addClass("textbox-focused");
			});
			for ( var _25 in opts.inputEvents) {
				tbText.bind(_25 + ".textbox", {
					target : target
				}, opts.inputEvents[_25]);
			}
		}
		var addon = tb.find(".textbox-addon");
		addon.unbind().bind("click",{target : target},function(e) {
			var icon = $(e.target).closest("a.textbox-icon:not(.textbox-icon-disabled)");
			if (icon.length) {
				var index = parseInt(icon.attr("icon-index"));
				var _icon = opts.icons[index];
				if (_icon && _icon.handler) {
					_icon.handler.call(icon[0], e);
					opts.onClickIcon.call(target, index);
				}
			}
		});
		addon.find(".textbox-icon").each(function(index) {
			var _icon = opts.icons[index];
			var iconHtml = $(this);
			if (!_icon || _icon.disabled || opts.disabled || opts.readonly) {
				iconHtml.addClass("textbox-icon-disabled");
			} else {
				iconHtml.removeClass("textbox-icon-disabled");
			}
		});
		tb.find(".textbox-button").linkbutton((opts.disabled || opts.readonly) ? "disable" : "enable");
	};
	function setDisplayState(target, state) {
		var textbox = $.data(target, "textbox");
		var opts = textbox.options;
		var tb = textbox.textbox;
		if (state) {
			opts.disabled = true;
			$(target).attr("disabled", "disabled");
			tb.find(".textbox-text,.textbox-value").attr("disabled", "disabled");
		} else {
			opts.disabled = false;
			$(target).removeAttr("disabled");
			tb.find(".textbox-text,.textbox-value").removeAttr("disabled");
		}
	};
	function readonly(target, _32) {
		var textbox = $.data(target, "textbox");
		var opts = textbox.options;
		opts.readonly = _32 == undefined ? true : _32;
		var _35 = textbox.textbox.find(".textbox-text");
		_35.removeAttr("readonly").removeClass("textbox-text-readonly");
		if (opts.readonly || !opts.editable) {
			_35.attr("readonly", "readonly").addClass("textbox-text-readonly");
		}
	};
	$.fn.textbox = function(options, param) {
		if (typeof options == "string") {
			var method = $.fn.textbox.methods[options];
			if (method) {
				return method(this, param);
			} else {
				return this.each(function() {
					var textbox = $(this).textbox("textbox");
					textbox.validatebox(options, param);
				});
			}
		}
		options = options || {};
		return this.each(function() {
			var textbox = $.data(this, "textbox");
			if (textbox) {
				$.extend(textbox.options, options);
				if (options.value != undefined) {
					textbox.options.originalValue = options.value;
				}
			} else {
				textbox = $.data(this, "textbox", {
					options : $.extend({}, $.fn.textbox.defaults, $.fn.textbox.parseOptions(this), options),
					textbox : createTextbox(this)
				});
				textbox.options.originalValue = textbox.options.value;
			}
			initTextbox(this);
			bindEvent(this);
			resize(this);
			validatebox(this);
			$(this).textbox("initValue", textbox.options.value);
		});
	};
	$.fn.textbox.methods = {
		options : function(jq) {
			return $.data(jq[0], "textbox").options;
		},
		textbox : function(jq) {
			return $.data(jq[0], "textbox").textbox.find(".textbox-text");
		},
		button : function(jq) {
			return $.data(jq[0], "textbox").textbox.find(".textbox-button");
		},
		destroy : function(jq) {
			return jq.each(function() {
				destroy(this);
			});
		},
		resize : function(jq, _3b) {
			return jq.each(function() {
				resize(this, _3b);
			});
		},
		disable : function(jq) {
			return jq.each(function() {
				setDisplayState(this, true);
				bindEvent(this);
			});
		},
		enable : function(jq) {
			return jq.each(function() {
				setDisplayState(this, false);
				bindEvent(this);
			});
		},
		readonly : function(jq, _3c) {
			return jq.each(function() {
				readonly(this, _3c);
				bindEvent(this);
			});
		},
		isValid : function(jq) {
			return jq.textbox("textbox").validatebox("isValid");
		},
		clear : function(jq) {
			return jq.each(function() {
				$(this).textbox("setValue", "");
			});
		},
		setText : function(jq, _3d) {
			return jq.each(function() {
				var _3e = $(this).textbox("options");
				var _3f = $(this).textbox("textbox");
				if ($(this).textbox("getText") != _3d) {
					_3e.value = _3d;
					_3f.val(_3d);
				}
				if (!_3f.is(":focus")) {
					if (_3d) {
						_3f.removeClass("textbox-prompt");
					} else {
						_3f.val(_3e.prompt).addClass("textbox-prompt");
					}
				}
				$(this).textbox("validate");
			});
		},
		initValue : function(jq, _40) {
			return jq.each(function() {
				var opt = $.data(this, "textbox").options;
				var _41 = $.data(this, "textbox");
				_41.options.value = "";
				//重新 编码
				if(opt.regChars && opt.regChars.length>0){
					_40= parser(this,_40);
				}
				$(this).textbox("setText", _40);
				_41.textbox.find(".textbox-value").val(_40);
				$(this).val(_40);
			});
		},
		setValue : function(jq, _42) {
			return jq.each(function() {
				var _43 = $.data(this, "textbox").options;
				var _44 = $(this).textbox("getValue");
				$(this).textbox("initValue", _42);
				if (_44 != _42) {
					_43.onChange.call(this, _42, _44);
				}
			});
		},
		getText : function(jq) {
			var textbox = jq.textbox("textbox");
			if (textbox.is(":focus")) {
				return textbox.val();
			} else {
				return jq.textbox("options").value;
			}
		},
		getValue : function(jq) {
			return jq.data("textbox").textbox.find(".textbox-value").val();
		},
		reset : function(jq) {
			return jq.each(function() {
				var _46 = $(this).textbox("options");
				$(this).textbox("setValue", _46.originalValue);
			});
		},
		getIcon : function(jq, _47) {
			return jq.data("textbox").textbox.find(".textbox-icon:eq(" + _47 + ")");
		},
		getTipX : function(jq) {
			var _48 = jq.data("textbox");
			var _49 = _48.options;
			var tb = _48.textbox;
			var _4a = tb.find(".textbox-text");
			var _4b = tb.find(".textbox-addon")._outerWidth();
			var _4c = tb.find(".textbox-button")._outerWidth();
			if (_49.tipPosition == "right") {
				return (_49.iconAlign == "right" ? _4b : 0)
						+ (_49.buttonAlign == "right" ? _4c : 0) + 1;
			} else {
				if (_49.tipPosition == "left") {
					return (_49.iconAlign == "left" ? -_4b : 0)
							+ (_49.buttonAlign == "left" ? -_4c : 0) - 1;
				} else {
					return _4b / 2 * (_49.iconAlign == "right" ? 1 : -1);
				}
			}
		}
	};
	$.fn.textbox.parseOptions = function(target) {
		var t = $(target);
		return $.extend({}, $.fn.validatebox.parseOptions(target), $.parser.parseOptions(target, 
				[ "prompt", "iconCls", "iconAlign",
				"buttonText", "buttonIcon", "buttonAlign", {
					multiline : "boolean",
					editable : "boolean",
					iconWidth : "number"
				} ]), {
			value : (t.val() || undefined),
			type : (t.attr("type") ? t.attr("type") : undefined),
			disabled : (t.attr("disabled") ? true : undefined),
			readonly : (t.attr("readonly") ? true : undefined)
		});
	};
	$.fn.textbox.defaults = $.extend({}, $.fn.validatebox.defaults, {
		width : "auto",
		height : 22,
		prompt : "",
		value : "",
		type : "text",
		multiline : false,
		editable : true,
		disabled : false,
		readonly : false,
		icons : [],
		regChars:["<",">","~","!","@","$","%","^","&","*","(",")","{","=","+","}","[","]","\"","`","/","'"],
		iconCls : null,
		iconAlign : "right",
		iconWidth : 18,
		buttonText : "",
		buttonIcon : null,
		buttonAlign : "right",
		inputEvents : {
			keypress : function(e) {
				var _14 = e.data.target;
				var _15 = $(_14).textbox("options");
				return _15.filter.call(_14, e);
			},
			blur : function(e) {
				var t = $(e.data.target);
				var _4e = t.textbox("options");
				t.textbox("setValue", _4e.value);
			}
		},
		filter : function(e) {
			var isExist=checkChar(this,e.which);
			if (e.ctrlKey == true && isExist) {
				return false;
			}else if(e.shiftKey == true && isExist){
				return false;
			} else if(isExist){
				return false;
			} else{
				return true;
			}
		},
		onChange : function(_4f, _50) {
		},
		onResize : function(_51, _52) {
		},
		onClickButton : function() {
		},
		onClickIcon : function(_53) {
		},
		onClick : function(_53) {
		}
	});
})(jQuery);
